find_package(PkgConfig)
if (PkgConfig_FOUND)
        pkg_search_module( PC_GLIB2 glib-2.0 )
#        pkg_check_modules(GLIB2 IMPORTED_TARGET glib-2.0)
endif()

find_library(GLIB2_LIBRARY
             NAMES glib-2.0
             HINTS ${PC_GLIB2_LIBDIR} ${GLIB2_ROOT_DIR}/lib)

find_library(GIO_LIBRARY
             NAMES gio-2.0
             HINTS ${PC_GLIB2_LIBDIR} ${GLIB2_ROOT_DIR}/lib)

find_library(GOBJECT_LIBRARY
             NAMES gobject-2.0
             HINTS ${PC_GLIB2_LIBDIR} ${GLIB2_ROOT_DIR}/lib)

find_path( GLIB2_MAIN_INCLUDE_DIR
        NAMES
                glib.h
        HINTS
                "${PC_GLIB2_INCLUDEDIR}"
                "${GLIB2_ROOT_DIR}/include"
        PATH_SUFFIXES
                glib-2.0
                glib-2.0/include
        PATHS
                /opt/gnome/include
                /opt/local/include
                /sw/include
                /usr/include
                /usr/local/include
)

find_program(GLIB_MKENUMS glib-mkenums
        HINTS ${PC_GLIB2_mkenums})

get_filename_component( glib2LibDir "${GLIB2_LIBRARY}" PATH)

find_path( GLIB2_INTERNAL_INCLUDE_DIR
        NAMES
                glibconfig.h
        HINTS
                "${PC_GLIB2_LIBDIR}"
                "${glib2LibDir}"
                "${GLIB2_INCLUDEDIR}"
                ${CMAKE_SYSTEM_LIBRARY_PATH}
        PATH_SUFFIXES
                glib-2.0/include
)

if(PC_GLIB2_VERSION)
        set(GLIB2_VERSION ${PC_GLIB2_VERSION})
elseif(GLIB2_INTERNAL_INCLUDE_DIR)
        # On systems without pkg-config (e.g. Windows), search its header
        # (available since the initial commit of GLib).
        file(STRINGS ${GLIB2_INTERNAL_INCLUDE_DIR}/glibconfig.h GLIB_MAJOR_VERSION
                REGEX "#define[ ]+GLIB_MAJOR_VERSION[ ]+[0-9]+")
        string(REGEX MATCH "[0-9]+" GLIB_MAJOR_VERSION ${GLIB_MAJOR_VERSION})
        file(STRINGS ${GLIB2_INTERNAL_INCLUDE_DIR}/glibconfig.h GLIB_MINOR_VERSION
                REGEX "#define[ ]+GLIB_MINOR_VERSION[ ]+[0-9]+")
        string(REGEX MATCH "[0-9]+" GLIB_MINOR_VERSION ${GLIB_MINOR_VERSION})
        file(STRINGS ${GLIB2_INTERNAL_INCLUDE_DIR}/glibconfig.h GLIB_MICRO_VERSION
                REGEX "#define[ ]+GLIB_MICRO_VERSION[ ]+[0-9]+")
        string(REGEX MATCH "[0-9]+" GLIB_MICRO_VERSION ${GLIB_MICRO_VERSION})
        set(GLIB2_VERSION ${GLIB_MAJOR_VERSION}.${GLIB_MINOR_VERSION}.${GLIB_MICRO_VERSION})
else()
        # When using VERSION_VAR it must be set to a valid value or undefined to
        # mean "not found". It's not enough to use the empty string or any other CMake false boolean.
        unset(GLIB2_VERSION)
endif()

include( FindPackageHandleStandardArgs )
find_package_handle_standard_args( MyGlib2
        REQUIRED_VARS   GLIB2_LIBRARY GLIB2_MAIN_INCLUDE_DIR GLIB2_INTERNAL_INCLUDE_DIR
        VERSION_VAR     GLIB2_VERSION
)

if( MyGlib2_FOUND )
        set( GLIB2_LIBRARIES ${GLIB2_LIBRARY} )
        # Include transitive dependencies for static linking.
        if(UNIX AND CMAKE_FIND_LIBRARY_SUFFIXES STREQUAL ".a")
                find_library(PCRE_LIBRARY pcre)
                list(APPEND GLIB2_LIBRARIES -pthread ${PCRE_LIBRARY})
        endif()
        set( GLIB2_INCLUDE_DIRS ${GLIB2_MAIN_INCLUDE_DIR} )
        set( GLIB2_INCLUDE_DIRS ${GLIB2_INCLUDE_DIRS} ${GLIB2_INTERNAL_INCLUDE_DIR} )
        message("bybobbi haha GLIB2_INCLUDE_DIRS is " ${GLIB2_INCLUDE_DIRS})
#        set( GLIB2_INCLUDE_DIRS ${GLIB2_MAIN_INCLUDE_DIR} ${GLIB2_INTERNAL_INCLUDE_DIR} )
        if ( GLIB2_FOUND )
            set ( GLIB2_DLL_DIR "${GLIB2_ROOT_DIR}/bin" )
            set ( GLIB2_LIB_DIR "${GLIB2_ROOT_DIR}/lib" )
            set ( GLIB2_DLLS
                    "${GLIB2_DLL_DIR}/glib-2.0-0.dll"
                    "${GLIB2_DLL_DIR}/gio-2.0-0.dll"
                    "${GLIB2_DLL_DIR}/gobject-2.0-0.dll"
                    "${GLIB2_DLL_DIR}/gmodule-2.0-0.dll"
                    "${GLIB2_DLL_DIR}/gthread-2.0-0.dll"
                    "${GLIB2_DLL_DIR}/intl-8.dll"
                    "${GLIB2_DLL_DIR}/pcre2-8-0.dll"
            )

            set ( GLIB2_LIBS
                    "${GLIB2_LIB_DIR}/glib-2.0.lib"
                    "${GLIB2_LIB_DIR}/gio-2.0.lib"
                    "${GLIB2_LIB_DIR}/gobject-2.0.lib"
                    "${GLIB2_LIB_DIR}/gmodule-2.0.lib"
                    "${GLIB2_LIB_DIR}/gthread-2.0.lib"
                    "${GLIB2_LIB_DIR}/intl.lib"
                    "${GLIB2_LIB_DIR}/pcre2-8.lib"
            )

            set ( GLIB2_PDBS
                    "${GLIB2_DLL_DIR}/glib-2.0-0.pdb"
                    "${GLIB2_DLL_DIR}/gio-2.0-0.pdb"
                    "${GLIB2_DLL_DIR}/gobject-2.0-0.pdb"
                    "${GLIB2_DLL_DIR}/gmodule-2.0-0.pdb"
                    "${GLIB2_DLL_DIR}/gthread-2.0-0.pdb"
            )
	endif()
elseif( MyGlib2_FIND_REQUIRED )
	message( SEND_ERROR "Package required but not found" )
else()
        set( GLIB2_LIBRARIES )
        set( GLIB2_MAIN_INCLUDE_DIRS )
        set( GLIB2_DLL_DIR )
        set( GLIB2_PDBS )
	set( GLIB2_DLLS )
endif()
